<?php if (!defined('BASEPATH')) {
	exit('No direct script access allowed');
}

class Account extends CI_Controller {

	public function __construct() {
		parent::__construct();
		$this->load->model('wus');
		$this->load->model($this->session->userdata('formodel'));
		$this->load->library('upload');
		$this->load->library('variable');
		$this->load->library('zend');
        //load in folder Zend
        $this->zend->load('Zend/Barcode');
		
	}

	public function index() {
		$this->load->view('layouts/header', $this->variable->load_data($this->session->userdata('title')));
		$this->load->view($this->session->userdata('formain'));
		$this->load->view('layouts/footer');
	}

	public function get_pending_wo(){
		echo json_encode($this->account_model->get_wo_pend($this->input->post('account_id')));
	}

	public function add_form() {
		$this->load->view($this->session->userdata('foradd'), array(
			'entty_type' => $this->account_model->get_e_type(),
			'meter_size' => $this->account_model->get_lookup('METER_SIZE'),
			'acct_type' => $this->account_model->get_acc_type(),
			'acct_stats' => $this->account_model->get_acc_stats(),
			'addr_pur' => $this->account_model->get_addr_pur(),
			'devtype' => $this->account_model->get_dev_type(),
			'devstats' => $this->account_model->get_dev_stats(),
			'item' => $this->account_model->get_item(),
			'country' => $this->account_model->countries(),
			'chrglist' => $this->account_model->getChrglst(),
			'cycleunit' => $this->account_model->getcycleunit(),
			'enttys' => $this->account_model->getSearch('%', ''),
			'acctnum' => $this->account_model->getcuracctnum(array(':p_val' => "ACCT_NUM")),
			'zone1' => $this->account_model->getzoning('ZONE1'),
			'zone2' => $this->account_model->getzoning('ZONE2'),
			'zone3' => $this->account_model->getzoning('ZONE3'),
			'zone4' => $this->account_model->getzoning('ZONE4'),
			'zone5' => $this->account_model->getzoning('ZONE5'),
			'zone' => $this->account_model->getzoning2('ZONE1'),
		));
	}

	public function edit_form($id) {
		$this->load->view($this->session->userdata('foredit'), array(
			'entity' 		=> $this->account_model->getEntity($id),
			'meter_size' => $this->account_model->get_lookup('METER_SIZE'),
			'entty_type' 	=> $this->account_model->get_e_type(),
			'addr' 			=> $this->account_model->getAddr($id, 'MAIN'),
			'addr2' 		=> $this->account_model->getAddr2($id),
			'acct' 			=> $this->account_model->getacct($id),
			'addr_pur' 		=> $this->account_model->get_addr_pur(),
			'acct_type' 	=> $this->account_model->get_acc_type(),
			'acct_stats' 	=> $this->account_model->get_acc_stats(),
			'devtype'		=> $this->account_model->get_dev_type(),
			'devstats' 		=> $this->account_model->get_dev_stats(),
			'item' 			=> $this->account_model->get_item(),
			'acct2' 		=> $this->account_model->getacct2($id),
			'country' 		=> $this->account_model->countries(),
			'chrglist' 		=> $this->account_model->getChrglst(),
			'cycleunit' 	=> $this->account_model->getcycleunit(),
			'mom_meter'		=> $this->account_model->get_mother_meter(),
			'zone1' => $this->account_model->getzoning('ZONE1'),
			'zone2' => $this->account_model->getzoning('ZONE2'),
			'zone3' => $this->account_model->getzoning('ZONE3'),
			'zone4' => $this->account_model->getzoning('ZONE4'),
			'zone5' => $this->account_model->getzoning('ZONE5'),
			'zone' => $this->account_model->getzoning2('ZONE1'),
		));
	}

	public function view_form($id) {
		$this->load->view($this->session->userdata('forview'), array(
			'entity' => $this->account_model->getEntity($id),
			'meter_size' => $this->account_model->get_lookup('METER_SIZE'),
			'entty_type' => $this->account_model->get_e_type(),
			'addr' => $this->account_model->getAddr($id, 'MAIN'),
			'addr2' => $this->account_model->getAddr2($id),
			'acct' => $this->account_model->getacct($id),
			'acct2' => $this->account_model->getacct2($id),
			'chglst' => $this->account_model->getChrglstId($id),
			'zone1' => $this->account_model->getzoning('ZONE1'),
			'zone2' => $this->account_model->getzoning('ZONE2'),
			'zone3' => $this->account_model->getzoning('ZONE3'),
			'zone4' => $this->account_model->getzoning('ZONE4'),
			'zone5' => $this->account_model->getzoning('ZONE5'),
			'zone' => $this->account_model->getzoning2('ZONE1'),
		));
	}

	public function insert() {
		//$mname = ucfirst($this->input->post('middle_name'));
		// $tmp = explode('/', $this->input->post('dob'));
		// $month = $tmp[0];
		// $year = $tmp[2];
		// $day = $tmp[1];
		// if($this->input->post('entity_type') == "PERS"){
		// 	$entity_name = ucfirst($this->input->post('last_name')).", ".ucfirst($this->input->post('first_name'))." ".$mname[0].".";
		// }else{
		// 	$entity_name = $this->input->post('entity_name');
		// }

		$data = array(
			':p_entity_type' => $this->input->post('entity_type'),
			':p_entity_name' => $this->input->post('entity_name'),
			':p_tax_reg_num' => $this->input->post('tax_reg_num'),
			':p_title' => $this->input->post('title'),
			':p_last_name' => $this->input->post('last_name'),
			':p_middle_name' => $this->input->post('middle_name'),
			':p_first_name' => $this->input->post('first_name'),
			':p_date_of_birth' => date($this->input->post('dob')),
			':p_enable_flag' => ($this->input->post('ena_flag') != '' ? 'Y' : 'N'),
			':p_created_by' => $this->session->userdata('user_id'),
		);
		$this->account_model->insertData($data);
	}


	public function deleteAccount(){

		$data = array(
			':p_account_id' =>	$this->input->post('acc_id'),	
			':p_entity_id'	=>	$this->input->post('ent_id'),
			':p_acct_num'	=>	$this->input->post('acc_num'),
			':p_acct_name'	=>	$this->input->post('acc_name'),
			':p_user_id'	=>	$this->session->userdata('user_id')
			);


		$this->account_model->deleteData($data);

	}

	public function update() {

		$data = array(
			':p_entity_type' => $this->input->post('entity_type'),
			':p_entity_name' => $this->input->post('entity_name'),
			':p_tax_reg_num' => $this->input->post('tax_reg_num'),
			':p_title' => $this->input->post('title'),
			':p_last_name' => $this->input->post('last_name'),
			':p_middle_name' => $this->input->post('middle_name'),
			':p_first_name' => $this->input->post('first_name'),
			':p_date_of_birth' => $this->input->post('dob'),
			':p_enable_flag' => ($this->input->post('ena_flag') != '' ? 'Y' : 'N'),
			':p_created_by' => $this->session->userdata('user_id'),
			':p_entity_id' => $this->input->post('entity_id'),
			':p_acct_num' => $this->input->post('acct_num'),
		);
		$this->account_model->updateData($data);
		//var_dump($data);
		//echo json_encode(array('err' => 0));
	}



	public function insert_addr() {

		$ena = ($this->input->post('ena_flag_a') != '' ? 'Y' : 'N');

		$data = array(
			':p_addr_code' => $this->input->post('addr_code'),
			':p_address_1' => $this->input->post('addr_1'),
			':p_address_2' => $this->input->post('addr_2'),
			':p_address_3' => $this->input->post('addr_3'),
			':p_city_town' => $this->input->post('city_town'),
			':p_postal_code' => $this->input->post('postal_code'),
			':p_state_province' => $this->input->post('state_province'),
			':p_country' => $this->input->post('country'),
			':p_phone_1' => $this->input->post('phone_1'),
			':p_phone_2' => $this->input->post('phone_2'),
			':p_phone_3' => $this->input->post('phone_3'),
			':p_purpose' => $this->input->post('purpose'),
			':p_enable_flag' => $ena,
			':p_zone_1' => $this->input->post('zone_1'),
			':p_zone_2' => $this->input->post('zone_2'),
			':p_zone_3' => $this->input->post('zone_3'),
			':p_zone_4' => $this->input->post('zone_4'),
			':p_zone_5' => $this->input->post('zone_5'),
			':p_created_by' => $this->session->userdata('user_id'),
			':p_updated_by' => $this->session->userdata('user_id'),
			':p_entity_id' => $this->input->post('entity_id'),
			':p_address_id' => $this->input->post('address_id'),
		);
		//var_dump($data);
		$this->account_model->insertDataAddr($data);
	}

	public function update_addr() {

		$ena = ($this->input->post('ena_flag_a') != '' ? 'Y' : 'N');

		$data = array(
			':p_addr_code' => $this->input->post('addr_code'),
			':p_address_1' => $this->input->post('addr_1'),
			':p_address_2' => $this->input->post('addr_2'),
			':p_address_3' => $this->input->post('addr_3'),
			':p_city_town' => $this->input->post('city_town'),
			':p_postal_code' => $this->input->post('postal_code'),
			':p_state_province' => $this->input->post('state_province'),
			':p_country' => $this->input->post('country'),
			':p_phone_1' => $this->input->post('phone_1'),
			':p_phone_2' => $this->input->post('phone_2'),
			':p_phone_3' => $this->input->post('phone_3'),
			':p_purpose' => $this->input->post('purpose'),
			':p_enable_flag' => $ena,
			':p_zone_1' => $this->input->post('zone_1'),
			':p_zone_2' => $this->input->post('zone_2'),
			':p_zone_3' => $this->input->post('zone_3'),
			':p_zone_4' => $this->input->post('zone_4'),
			':p_zone_5' => $this->input->post('zone_5'),
			':p_updated_by' => $this->session->userdata('user_id'),
			':p_entity_id' => $this->input->post('entity_id'),
			':p_address_id' => $this->input->post('address_id'),
		);
		//var_dump($data);
		$this->account_model->updateDataAddr($data);
	}

	public function insert_acc() {
		$data = array(
			':p_acct_num' 	  => $this->input->post('acct_num'),
			':p_acct_name'	  => $this->input->post('acct_name'),
			':p_acct_type' 	  => $this->input->post('acct_type'),
			':p_attention_to' => $this->input->post('attention_to'),
			':p_acct_addr' 	  => $this->input->post('acct_addr'),
			':p_bill_to_addr' => $this->input->post('bill_to_addr'),
			':p_active_start' => $this->input->post('active_start'),
			':p_active_end'   => $this->input->post('active_end'),
			':p_latitude'	  => $this->input->post('nlat'),
			':p_longitude'    => $this->input->post('nlng'),
			':p_status' 	  => $this->input->post('status'),
			':p_ena_flag' 	  => ($this->input->post('ena_flag') != "" ? 'Y' : 'N'),
			':p_created_by'   => $this->session->userdata('user_id'),
			':p_updated_by'   => $this->session->userdata('user_id'),
			':p_entity_id'    => $this->input->post('entity_id'),
			':p_meter_size'	  => $this->input->post('meter_size'),
			':p_meter_seq'	  => $this->input->post('meter_seq')
		);
		//var_dump($data);
		$this->account_model->insertDataAcc($data);
	}

	public function create_wo(){
		$data = array(
				':p_maintain_code' 	=> 'NEW',
				':p_account_id' 	=> $this->input->post('acct_id'),
				':p_status' 		=> 'Pending',
				':p_target_date' 	=> $this->input->post('target_date'),
				':p_assigned_to' 	=> $this->account_model->getwoowner('DEFAULT_WO_OWNER'),
				':p_user_id' 		=> $this->session->userdata('user_id'),
			);
			$this->account_model->insert_work_order($data);
		
	}

	
	public function update_acc() {

 		//$this->barcode($this->input->post('acct_num_b'));."";
 		//$code = "".."";
		//$code = (string)$this->input->post('acct_num_b');
		
		 //
   		
        //generate barcode
        
       	//var_dump($wah);

		$data = array(
			':p_acct_name' => $this->input->post('acct_name'),
			':p_acct_type' => $this->input->post('acct_type'),
			':p_attention_to' => $this->input->post('attention_to'),
			':p_acct_addr' => $this->input->post('acct_addr'),
			':p_bill_to_addr' => $this->input->post('bill_to_addr'),
			':p_active_start' => $this->input->post('active_start'),
			':p_active_end' => $this->input->post('active_end'),
			':p_latitude'	=> $this->input->post('edit_lat'),
			':p_longitude'	=> $this->input->post('edit_lng'),
			':p_status' => "PND",
			':p_ena_flag' => ($this->input->post('ena_flag') != "" ? 'Y' : 'N'),
			':p_updated_by' => $this->session->userdata('user_id'),
			':p_entity_id' => $this->input->post('entity_id'),
			':p_account_id' => $this->input->post('account_id'),
			':p_meter_size'	  => $this->input->post('meter_size'),
			':p_acct_num'	  => $this->input->post('acct_num'),
			':p_meter_seq'	  => $this->input->post('meter_seq'),
		);
		//var_dump($data);
		//$barcode = $this->input->post('acct_num_b');
		//var_dump();
		
		
		$this->account_model->updateDataAcc($data);


	}
	public function generate($acct,$sn){
		
		   //load library

        $barcode = $sn ;

        $imageResource = Zend_Barcode::factory('code128', 'image', array('text'=>$barcode), array('imageType' => 'png'))->draw();
        imagepng($imageResource, 'assets/barcodes/'.$acct.'.png');

	}
	public function insert_dev() {
		$data = array(
			':p_item_id' => $this->input->post('item_num'),
			':p_device_type' => $this->input->post('dev_type'),
			':p_model_number' => $this->input->post('model_number'),
			':p_serial_number' => $this->input->post('serial_number'),
			':p_device_id' => $this->input->post('dev_id'),
			':p_install_date' => $this->input->post('install_date'),
			':p_warranty_period' => $this->input->post('warranty_per'),
			':p_initial_reading' => $this->input->post('initial_read'),
			':p_last_reading' => $this->input->post('last_read'),
			':p_maintenance_cycle' => $this->input->post('maint_cycle'),
			':p_replacement_cycle' => $this->input->post('repl_cycle'),
			':p_maintenance_cycle_unit' => $this->input->post('maint_cycle_unit'),
			':p_replacement_cycle_unit' => $this->input->post('repl_cycle_unit'),
			':p_status' => $this->input->post('status'),
			':p_device_remarks' => $this->input->post('dev_remarks'),
			':p_created_by' => $this->session->userdata('user_id'),
			':p_updated_by' => $this->session->userdata('user_id'),
			':p_account_id' => $this->input->post('account_id'),
		);
		//var_dump($data);
		$this->account_model->insertDataDev($data);
		$this->generate($this->input->post('account_num'),$this->input->post('serial_number'));
	}

	public function update_dev() {
		$data = array(
	
			':p_device_id' 				=> $this->input->post('dev_id'),
			':p_mmeter_id'				=> $this->input->post('mom_meter'),
			':p_latitude'  				=> $this->input->post('edit_lat_d'),
			':p_longitude' 				=> $this->input->post('edit_lng_d'),
			':p_install_date' 			=> $this->input->post('install_date'),
			':p_warranty_period' 		=> $this->input->post('warranty_per'),
			':p_initial_reading' 		=> $this->input->post('initial_read'),
			':p_last_reading' 			=> $this->input->post('last_read'),
			':p_maintenance_cycle' 		=> $this->input->post('maint_cycle'),
			':p_replacement_cycle' 		=> $this->input->post('repl_cycle'),
			':p_maintenance_cycle_unit' => $this->input->post('maint_cycle_unit'),
			':p_replacement_cycle_unit' => $this->input->post('repl_cycle_unit'),
			':p_status' 				=> $this->input->post('status'),
			':p_device_remarks' 		=> $this->input->post('dev_remarks'),
			':p_updated_by' 			=> $this->session->userdata('user_id'),
			':p_account_id' 			=> $this->input->post('account_id'),
			':p_device_assignment_id' 	=> $this->input->post('device_ass_id'),
		);
		//var_dump($data);
		$this->account_model->updateDataDev($data);
		$this->generate($this->input->post('account_num'),$this->input->post('serial_number'));
	}

	public function add_chglist() {
		$data = array(
			':p_account_id' => $this->input->post('account_id'),
			':p_list_id' => $this->input->post('list_id'),
			':p_start_date' => date($this->input->post('s_date')),
			':p_end_date' => date($this->input->post('e_date')),
			':p_enable_flag' => ($this->input->post('enable_flag') != '' ? 'Y' : 'N'),
			':p_created_by' => $this->session->userdata('user_id'),
			':p_updated_by' => $this->session->userdata('user_id'),
		);
		//var_dump($data);
		$this->account_model->insertChrglist($data);
	}

	public function upd_chrglist() {
		$data = array(
			':p_account_id' => $this->input->post('account_id'),
			':p_list_id' => $this->input->post('list_id'),
			':p_start_date' => date($this->input->post('s_date')),
			':p_end_date' => date($this->input->post('e_date')),
			':p_enable_flag' => ($this->input->post('enable_flag') != '' ? 'Y' : 'N'),
			':p_updated_by' => $this->session->userdata('user_id'),
			':p_acct_chrg_id' => $this->input->post('acct_chrg_id'),
		);
		$this->account_model->updateChrglist($data);
	}

	public function get_chglist() {
		echo json_encode($this->account_model->getchglist($this->input->post('account_id')));
	}

	public function get_chglist2() {
		echo json_encode($this->account_model->getChrglst());
	}

	public function get_device() {
		echo json_encode($this->account_model->getDevice($this->input->post('account_id')));
	}

	public function get_device2() {
		echo json_encode($this->account_model->getDevice2($this->input->post('device_assignment_id')));
	}

	public function get_ena_addr() {
		echo json_encode($this->account_model->getEnaAddr($this->input->post('entity_id')));
	}

	public function search() {
		echo json_encode($this->account_model->getSearch($this->input->post('entity_type'), $this->input->post('entity_name')));
	}

	public function searchaccount() {
		$que = $this->account_model->getSearchAcct($this->input->post('acct_num'), $this->input->post('acct_name'), $this->input->post('entity_name'));
		$tmp = '';
		foreach($que as $q){
			$tmp .= "<tr><td>".$q->entity_name."</td><td>".$q->acct_num."</td><td>".$q->acct_name."</td><td>".$q->meaning."</td><td>".$q->status."</td><td>".$q->enable_flag."</td><td></td></tr>";
		}

		echo json_encode($que);
	}

	public function get_entities() {
		echo json_encode($this->account_model->getEntities($this->input->post('entity_id')));
	}
	public function get_zones() {
		echo json_encode($this->account_model->getZones($this->input->post('entity_id'), $this->input->post('addr_code')));
	}

	public function getAddress() {
		echo json_encode($this->account_model->getAllAddr($this->input->post('entity_id'), $this->input->post('address_id')));
	}

	public function getAddress2() {
		echo json_encode($this->account_model->getAllAddr2($this->input->post('entity_id')));
	}

	public function getAccount() {
		echo json_encode($this->account_model->getAllAccount($this->input->post('entity_id'), $this->input->post('account_id'), $this->input->post('acct_addr')));
	}

	public function getAccount2() {
		echo json_encode($this->account_model->getacct($this->input->post('entity_id')));
	}

	public function getMeterSeq(){
		echo json_encode($this->account_model->getMeterSeq());
	}
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */